#-----------------------------------#
# Replication of:
# Seeking Opportunity in the Knowledge Economy: Moving Places, Moving Politics?
# Valentina Consiglio, Thomas Kurer

# File: B_master_R
# Note: This file is the master file through which all scripts coded in R can be executed. 

#-----------------------------------#

# Clear workspace
rm(list=ls())

if (!require("pacman")) install.packages("pacman")

library(pacman)
p_load(car, arm, lmtest, vars, ggplot2, psych, stargazer, dplyr, readxl, tidyverse, 
       haven, labelled, gtsummary, survey, srvyr, BMisc, questionr, plm, texreg, broom, fixest, modelsummary, bife, data.table)

#-------------------------------------------------------------------------------# 


#### Define paths, create folder structure, and set run mode ####
code_path <- "code/"
soep_path <- "../soep-data/soep.v37/Stata/"
data_original_path <- "data_original/"
data_created_path <- "data_created/"
graph_path <- "figures/" 
tables_path <- "tables/" 

# Creates folder structure if not existing (note: in this case, the data folders need to be filled manually with content from replication folders)
folders <-  c("code", "data_original", "data_created", "figures", "tables", 
              "tables/robustness_heterogeneity", "tables/mechanisms", 
              "tables/mechanisms/components",
              "data_original/shapes","data_original/cross-walks")



for(folder_name in folders) {
  if(!file.exists(paste0(folder_name))) {
    dir.create(paste0(folder_name))
  }
}


#Set run mode
data_room <- FALSE  
# Set TRUE for full replication in the data room;
# if FALSE, only scripts with plots will be executed as these are based on csv files in the replication folder.

#-------------------------------------------------------------------------------# 
#### A | Opportunity Index ####

# Create multidimensional Opportunity Index at the Kreis-level (NUTS-3)
# along with Figures/Tables for visualization, validation and sensitivity
source(paste0(code_path, '1_opportunity_index.R'))

#-------------------------------------------------------------------------------# 
#### B | Prepare data set ####

# Modify SOEP variables and merge and modify Opportunity Index 
if (data_room) { 
source(paste0(code_path, '3_dataprep'))
}

#-------------------------------------------------------------------------------# 
#### C | Analyse descriptive moving patterns ####

# Moving probability and mobving direction by socio-demographic characteristics
source(paste0(code_path, '4_plots.R'))
# Note: This script includes plots that can be run outside data room.

#-------------------------------------------------------------------------------# 
#### D | Main analysis ####

# Model preparation and estimation - main models #
source(paste0(code_path, '5_models_main_NUTS3.R'))
# Note: This script includes plots that can be run outside data room.

#-------------------------------------------------------------------------------# 
#### E | Robustness and Heterogeneity ####

# Runs robustness and heterogeneity analyses
if (data_room) { 
source(paste0(code_path, '6_models_rob-het.R'))
}

#-------------------------------------------------------------------------------# 
#### F | Mechanisms ####

# Runs analyses of mechanisms

# Opportunity Index (full) # 
if (data_room) { 
source(paste0(code_path, '7a_models_mechanisms.R'))
}

# PC1 - Economy # 
if (data_room) { 
source(paste0(code_path, '7b_models_mechanisms_PC1.R'))
}

# PC2 - Amenities # 
if (data_room) { 
source(paste0(code_path, '7c_models_mechanisms_PC2.R'))
}

#-------------------------------------------------------------------------------# 
#### G | Replication of main analysis at labor market region (LMR) level ####

# Reruns main analyses at level of 223 German LMRs
if (data_room) { 
source(paste0(code_path, '8_models_main_LMR.R'))
}
#-------------------------------------------------------------------------------# 
